package com.vpen.server.one.controller;

import com.vpen.server.one.model.ChatMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/11/26 19:47
 */
@Slf4j
@Controller
public class HelloController {

    @Resource
    private SimpMessagingTemplate template;

    @MessageMapping("/hello")
    public void hello(@Payload String chatMessage) {
        log.debug("收到消息：{}", chatMessage);
    }

    /**
     * 订阅发布模式，使用注解方式
     * 消息将会发送到这个broke的地址/topic/hello,所有订阅这个地址的人都能收到消息
     */
    @MessageMapping("hello1")
    @SendTo("/topic/hello")
    public String hello1(String msg) {
        log.debug("{}", msg);
        return "消息" + msg;
    }


    /**
     * 订阅发布模式，使用编码方式
     */
    @MessageMapping("/hello2")
    public void hello2(@Payload String chatMessage) {
        log.debug("收到消息：{}", chatMessage);
        template.convertAndSend("/topic/hello", chatMessage);
    }

    @SubscribeMapping("/subscribe")
    public String subscribe(@Payload String chatMessage) {
        log.debug("收到消息：{}", chatMessage);
        return "Server:" + chatMessage;
    }

}
